package com.wgchao.mdmp.service.impl;

import com.wgchao.mdmp.common.LoadData;
import com.wgchao.mdmp.common.aal.AalData;
import com.wgchao.mdmp.common.aal.GXYData;
import com.wgchao.mdmp.common.aal.IpMessage;
import com.wgchao.mdmp.common.aal.XTYData;
import com.wgchao.mdmp.dao.GxyDao;
import com.wgchao.mdmp.dao.XlDao;
import com.wgchao.mdmp.dao.XtDao;
import com.wgchao.mdmp.entity.MdmpGxy;
import com.wgchao.mdmp.entity.MdmpOriUp;
import com.wgchao.mdmp.entity.MdmpXl;
import com.wgchao.mdmp.entity.MdmpXt;
import com.wgchao.mdmp.repository.MdmpOriUpRepository;
import com.wgchao.mdmp.service.UpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: UpLoadServiceImpl
 * @author: wgchao
 * @createTime: 2019/11/3 2:40 PM
 * @Description: 上传数据业务类
 */
@Service
@Transactional
public class UpLoadServiceImpl implements UpLoadService {
    @Autowired
    private MdmpOriUpRepository oriUpRepository;
    @Autowired
    private GxyDao gxyDao;
    @Autowired
    private XtDao xtDao;
    @Autowired
    private XlDao xlDao;

    /**
     * 愛奧樂 数据上传
     * @param data 检测数据
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public String aalUpload(String data, HttpServletRequest request) throws Exception {
    //    System.out.println("上传数据："+data);
        AalData ad = AalData.getInitializtion(data);
        if(!isSum(ad)){
            System.out.println("检校码错误");
            return "+IP"+IpMessage.getIpOf16(request)+IpMessage.getDateOf16(new Date())+"ER";
        }
        if(aalUploadData(ad, data)){
            System.out.println(IpMessage.getIpOf16(request));
            return "+IP"+IpMessage.getIpOf16(request)+IpMessage.getDateOf16(new Date())+"OK";
        }else{
            return "+IP"+IpMessage.getIpOf16(request)+IpMessage.getDateOf16(new Date())+"ER";
        }
    }

    public boolean aalUploadData(AalData ad, String oridata) throws Exception{
        return load(ad, data -> {
            AalData aalData = (AalData) data;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date measureTime = sdf.parse("20"+aalData.getYear()+"-"+aalData.getMonth()+"-"+aalData.getDay()+" "+
            aalData.getHour()+":"+aalData.getMinute()+":00");
            //TODO 获取设备用户信息
            String idcard = "1111";

            //保存原始数据
            MdmpOriUp oriUp = new MdmpOriUp();
            oriUp.setIdcard(idcard);
            oriUp.setMeasureTime(measureTime);
            oriUp.setOriData(oridata);
            oriUp.setSbbs("aal");
            oriUp.setSbbh(ad.getImsi());
            oriUp.setUpTime(new Date());
            oriUp = oriUpRepository.save(oriUp);

            //保存检测数据
            if(ad.getDeviceType().equals("01")){//血压计
                GXYData gxyData = (GXYData) ad.getData();
                //血压值
                MdmpGxy gxy = new MdmpGxy();
                gxy.setCreateTime(new Date());
                gxy.setIdcard(idcard);
                gxy.setMeasureTime(measureTime);
                gxy.setSs(Double.valueOf(gxyData.getGy()));
                gxy.setSz(Double.valueOf(gxyData.getDy()));
                gxy.setUpTime(oriUp.getUpTime());
                gxy = gxyDao.save(gxy);
                //心率
                MdmpXl xl = new MdmpXl();
                xl.setCreateTime(new Date());
                xl.setIdcard(idcard);
                xl.setMeasureTime(measureTime);
                xl.setUpTime(oriUp.getUpTime());
                xl.setXl(Double.valueOf(gxyData.getXlv()));
                xl = xlDao.save(xl);
            }else if(ad.getDeviceType().equals("02")){//血糖仪
                XTYData xtyData = (XTYData) ad.getData();
                MdmpXt xt = new MdmpXt();
                xt.setCreateTime(new Date());
                xt.setIdcard(idcard);
                xt.setMeasureTime(measureTime);
                xt.setUpTime(oriUp.getUpTime());
                xt.setGlu(Double.valueOf(xtyData.getXt()));
                xt = xtDao.save(xt);
            }
            return true;
        });
    }


    /**
     * 检校
     * @param ad
     * @return
     */
    private boolean isSum(AalData ad){
        System.out.println("开始检校");
        String str = ad.getDatastr();
        if(ad.getSum().equals(IpMessage.getSum(str))){
            System.out.println("检校码正确");
            return true;
        }
        return false;
    }




    /**
     * 上传数据
     * @param data
     * @param loadData
     * @return
     */
    public boolean load(Object data, LoadData loadData) throws Exception{
        return loadData.load(data);
    }
}
